package com.homedo.microservice.open.service.gateway.manager.web.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.enums.ResponseEnum;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.exception.HomedoException;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.AlertNotice;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.AlertUser;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.AlertUserRouteService;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.AlertUserEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.query.AlertUserQuery;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.persistence.dao.AlertNoticeDao;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.persistence.dao.AlertUserDao;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.persistence.dao.AlertUserRouteServiceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年10月16日 16时44分30秒
 */
@Service
public class AlertUserService extends BaseService<AlertUser> {
    @Autowired
    private AlertUserDao alertUserDao;
    @Autowired
    private AlertNoticeDao alertNoticeDao;
    @Autowired
    private AlertUserRouteServiceDao alertUserRouteServiceDao;


    @Override
    public List<AlertUserEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<AlertUser> alertUsers = alertUserDao.getListByIds(ids, AlertUser.class);

        if (!Detect.notEmpty(alertUsers)) {
            return null;
        }

        return this.convent2Enhanceds(alertUsers);
    }


    @Override
    public List<AlertUserEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<AlertUser> alertUsers = alertUserDao.getListByIdsGreaterThanMark0(ids, AlertUser.class);

        if (!Detect.notEmpty(alertUsers)) {
            return null;
        }

        return this.convent2Enhanceds(alertUsers);
    }


    @Override
    public List<AlertUserEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<AlertUser> alertUsers = (List<AlertUser>) pos;
        List<AlertUserEnhanced> alertUserEnhanceds = Lists.newArrayList();
        alertUsers.forEach(e -> alertUserEnhanceds.add(new AlertUserEnhanced(e)));
        return alertUserEnhanceds;
    }


    public Pagination<AlertUserEnhanced> getPagination(AlertUserQuery alertUserQuery, Pagination<AlertUserEnhanced> pagination, UserContext userContext) {
        Pagination<AlertUser> alertUserPagination = alertUserDao.getPagination(alertUserQuery, pagination.copy());

        pagination.setCount(alertUserPagination.getCount());
        if (!Detect.notEmpty(alertUserPagination.getItems())) {
            return pagination;
        }

        pagination.setItems(this.convent2Enhanceds(alertUserPagination.getItems()));

        return pagination;
    }

    public AlertUser saveAlertUser(AlertUser alertUser, UserContext userContext) {
        this.checkSave(alertUser);

        alertUser.setCreateTime(new Date());
        alertUser.setUpdateTime(new Date());

        alertUser = alertUserDao.saveSelective(alertUser);

        return alertUser;
    }

    public void updateAlertUser(int id, String username, String comment, String phoneNo, String email, UserContext userContext) {
        this.checkUpdate(phoneNo, email);

        AlertUser alertUser = alertUserDao.getById(id);

        if (Detect.notEmpty(username)) {
            alertUser.setUsername(username);
        }

        if (Detect.notEmpty(comment)) {
            alertUser.setComment(comment);
        }

        if (Detect.notEmpty(phoneNo)) {
            alertUser.setPhoneNo(phoneNo);
        }

        if (Detect.notEmpty(email)) {
            alertUser.setEmail(email);
        }

        alertUser.setUpdateTime(new Date());

        alertUserDao.updateByPrimaryKeySelective(alertUser);
    }

    private void checkUpdate(String phoneNo, String email) {
        if (Detect.notEmpty(phoneNo) && !Detect.checkMobileNumber(phoneNo)) {
            throw new HomedoException(ResponseEnum.PARAM_ERROR);
        }
        if (Detect.notEmpty(email) && !Detect.checkEmail(email)) {
            throw new HomedoException(ResponseEnum.PARAM_ERROR);
        }
    }

    private void checkSave(AlertUser alertUser) {
        if (!Detect.notEmpty(alertUser.getApplicationName()) || !Detect.notEmpty(alertUser.getUsername())
                || !Detect.notEmpty(alertUser.getComment()) || !Detect.notEmpty(alertUser.getPhoneNo())
                || !Detect.notEmpty(alertUser.getEmail())) {
            throw new HomedoException(ResponseEnum.PARAM_ERROR);
        }

        if (!Detect.checkMobileNumber(alertUser.getPhoneNo()) || !Detect.checkEmail(alertUser.getEmail())) {
            throw new HomedoException(ResponseEnum.PARAM_ERROR);
        }
    }

    public void deleteAlertUser(int id, UserContext userContext) {
        alertUserDao.deleteById(id);

        AlertNotice alertNotice = new AlertNotice();
        alertNotice.setAlertUserId(id);
        alertNoticeDao.delete(alertNotice);

        AlertUserRouteService alertUserRouteService = new AlertUserRouteService();
        alertUserRouteService.setAlterUserId(id);
        alertUserRouteServiceDao.delete(alertUserRouteService);
    }

    public AlertUserEnhanced getEnhancedByUsername4Application(String username, String applicationName, UserContext userContext) {
        AlertUser alertUser = new AlertUser();
        alertUser.setApplicationName(applicationName);
        alertUser.setUsername(username);

        alertUser = alertUserDao.getOneByRecord(alertUser);
        if (alertUser == null) {
            return null;
        }

        return this.convent2Enhanced(alertUser);
    }
}
